package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.CollectionKit;
import com.wxxymaker.edu.model.entity.TestPaper;
import com.wxxymaker.edu.utils.FileUtils;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/10/11 21:52
 * @DESCRIBE
 */
@Bean
public class TestPaperService implements Service<TestPaper> {

    @Inject
    private CourseService courseService;

    /**
     * 获取该课程的试卷
     * @param kid 课程id
     * @param uid 当前登录用户id
     */
    public Page page(Integer kid, Integer uid, int page, int size) {
        Page<TestPaper> paperPage = Anima.select().from(TestPaper.class)
                .where(TestPaper::getCourseId, kid)
                .order(TestPaper::getTime, OrderBy.DESC)
                .page(page, size);
        if (CollectionKit.isNotEmpty(paperPage.getRows())) {
            paperPage.getRows().forEach(paper -> {
                if (!canDownResult(uid, kid, paper)) {
                    paper.setResult(null);
                }
            });
            return paperPage;
        }
        return null;
    }

    /**
     * 判断是否可以下载答案
     * @param uid 用户id
     * @param kid 课程id
     */
    public boolean canDownResult(int uid, int kid, TestPaper paper) {
        boolean myPublish = courseService.isMyPublish(kid, uid);
        if (myPublish) return true;
        return isEnd(paper);
    }

    private boolean isEnd(TestPaper paper) {
        String endTime = paper.getEndTime();
        return TimeUtils.isOverDate(endTime);
    }

    public boolean addSubmits(int pid) {
        return Anima.execute("UPDATE test_paper SET submits=submits+1 WHERE id=?", pid) > 0;
    }

    public boolean addDowns(int pid) {
        return Anima.execute("UPDATE test_paper SET downs=downs+1 WHERE id=?", pid) > 0;
    }

    @Override
    public TestPaper add(TestPaper testPaper) {
        testPaper.setTime(TimeUtils.getDate());
        testPaper.setDowns(0);
        testPaper.setSubmits(0);
        Integer integer = testPaper.save().asInt();
        if (integer > 0) {
            testPaper.setId(integer);
            return testPaper;
        }
        return null;
    }

    @Override
    public TestPaper delete(Object pk) {
        TestPaper testPaper = byId(pk);
        if (null != testPaper) {
            testPaper.delete();
            FileUtils.deleteByDownPath(testPaper.getPath(), 1);
            FileUtils.deleteByDownPath(testPaper.getResult(), 2);
            return testPaper;
        }
        return null;
    }

    @Override
    public boolean change(TestPaper testPaper) {
        if (testPaper.getId() > 0) {
            return testPaper.update() > 0;
        }
        return false;
    }

    @Override
    public TestPaper byId(Object pk) {
        return Anima.select().from(TestPaper.class).byId(pk);
    }


    public Long countByUid(Integer uid) {
        return Anima.select().from(TestPaper.class).where(TestPaper::getUserId, uid).count();
    }


    public int deleteByKid(Integer kid) {
        return Anima.delete().from(TestPaper.class).where(TestPaper::getCourseId, kid).execute();
    }
}
